//# sourceURL=dynamicScript.js
/**
 * Filename: d:\CODE\FTP\FTP_TEST\FTP\static\js\page_1.js
 * Path: d:\CODE\FTP\FTP_TEST\FTP\static\js
 * Created Date: Wednesday, May 11th 2022, 2:05:17 pm
 * Author: xiguan
 * 
 * Copyright (c) 2022 XiGuan Inc.
 */


const basic_url = "http://127.0.0.1:8880";

let current_syncdir_id = 0; // 当前同步目录编号
let current_server_port = 22; //当前监听端口
let current_interclock_id = 0; //当前同步定时器id

// 封装ajax请求

function ajax(params){
    let url = basic_url+params.url;
    let data = params.data;
    let method = "GET";
    if(params.method !== ""){
        method = params.method;
    }

    return $.ajax({
        url : url,
        data : data,
        method: method
    });
}

// 获取同步目录
async function get_all_syncdir() {
    $(".content1-syncdir-item").remove();
    let params = {
        url: "/sync/get_all"
    };
    await ajax(params).then(data => {
        let syncdirs = JSON.parse(data);
        if(syncdirs.length !== 0) {
            current_syncdir_id = syncdirs[0].id
            current_server_port = syncdirs[0].port
        }
        for(let i=0; i<syncdirs.length; i++) {
            str =   '<div class="content1-syncdir-item">' +
                        '<text class="content1-syncdir-item-no">同步编号：' + syncdirs[i].id +
                        '</text>' +
                        '<text class="content1-syncdir-item-port">侦听端口：' + syncdirs[i].port +
                        '</text>' +
                        '<text class="content1-syncdir-item-del" onclick="del_syncdir('+ syncdirs[i].id +')">×' +
                        '</text>' +
                        '<text class="content1-syncdir-item-path">同步目录：' + syncdirs[i].path +
                        '</text>' +
                    '</div>'
            $('#content1').append(str);
        }
        $("#sync-dir-select-div").html("");
        str2 = '<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">' +
                '同步目录<span class="caret"></span>' +
            '</button>' +
            '<ul class="dropdown-menu">'
        for(let i=0; i<syncdirs.length; i++) {
            str2 = str2 + '<li><a href="#" onclick="change_syncdir_port('+ syncdirs[i].id +','+ syncdirs[i].port +')">编号：' + syncdirs[i].id + ', 端口：'+ syncdirs[i].port+'</a></li>';
        }
        str2 = str2 + '</ul>';
        $('#sync-dir-select-div').append(str2);
    });
}

// 删除同步目录
async function del_syncdir(id) {
    // 确认框
    bootbox.confirm({
        message: "确认删除吗?",
        buttons: {
                confirm: {
                    label: '确定',
                },
                cancel: {
                    label: '取消',
                }
        },
        callback: async function (result) {
            if (result) {
                let params = {
                    url: "/sync/del_dir",
                    data: {
                        "id": id
                    }
                };
                await ajax(params).then(async data => {
                    var dialog = bootbox.dialog({
                        message: '<p class="text-center mb-0"><i class="fa fa-spin fa-cog"></i> 正在删除...</p>',
                        closeButton: false
                    });
                    setTimeout(async function () {
                        dialog.modal('hide');
                        await get_all_syncdir();
                    },2000);
                });
            }
        }
    });
}
 
 
$(async function(){
    // // 获得所有同步列表
    // await get_all_syncdir();
    console.info("hahaa1");
    await get_all_syncdir();
    // 点击新增
    $("#content1-add-btn").click(() =>{
        let path = $("#content1-add-path").val();
        let port = $("#content1-add-port").val();

        if(path === "" || port ===""){
            alert("路径与端口不能为空!");
        }
        else{
            async function add() {
                let params = {
                    url: "/sync/add_dir",
                    data: {
                        "path": $("#content1-add-path").val(),
                        "port": $("#content1-add-port").val()
                    }
                };
                await ajax(params).then(async data => {
                    if (data === "OK") {
                        var dialog = bootbox.dialog({
                            message: '<p class="text-center mb-0"><i class="fa fa-spin fa-cog"></i> 正在新增本地同步目录...</p>',
                            closeButton: false
                        });
                        setTimeout(async function (){
                            dialog.modal('hide');
                            await get_all_syncdir();
                        },2000);

                    }
                    else{
                        alert("添加失败!");
                    }
                })
            }
            add();
        }

    });

})